package cn.com.lms.business.exam.service;


import cn.com.lms.app.api.exam.dto.EmployeePaperSubjectAddDTO;
import cn.com.lms.app.api.exam.vo.EmployeePaperStageDetailVO;
import cn.com.lms.business.core.constant.LearningStateEnum;
import cn.com.lms.business.exam.enums.SubjectTypeEnum;
import cn.com.lms.business.exam.persistence.entity.EmployeePaper;
import cn.com.lms.business.exam.persistence.entity.EmployeePaperSubject;

import cn.com.lms.business.exam.persistence.entity.QuestionSubject;
import cn.com.lms.business.exam.persistence.repository.EmployeePaperSubjectRepository;
import cn.com.lms.business.exam.utils.JsonUtil;
import cn.com.lms.business.learning.persistence.entity.FbLearningPlan;
import cn.com.lms.business.learning.persistence.repository.LearningStageDetailRepository;
import cn.com.lms.business.learning.service.LearningPlanService;

import com.bnzj.core.persistence.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class EmployeePaperSubjectService extends BaseService<EmployeePaperSubjectRepository, EmployeePaperSubject, Long> {

    @Autowired
    EmployeePaperService employeePaperService;

    @Autowired
    EmployeePaperStageService employeePaperStageService;

    @Autowired
    private LearningStageDetailRepository stageDetailRepository;

    @Autowired
    private LearningPlanService planService;

    @Transactional(rollbackFor = Exception.class)
    public List<EmployeePaperSubject> savePaperSubject(List<EmployeePaperSubject> employeePaperSubjectList, EmployeePaper employeePaper, EmployeePaperStageDetailVO employeePaperStageDetailVO) {
        List<EmployeePaperSubject> employeePaperSubjects = new CopyOnWriteArrayList<>();
        EmployeePaper result = employeePaperService.save(employeePaper);
        if (!CollectionUtils.isEmpty(employeePaperSubjectList)) {
            List<Long> subjectIdList = new CopyOnWriteArrayList<>();
            employeePaperSubjectList.stream().forEach(m -> {
                m.setEmployeePaperId(result.getId());
                subjectIdList.add(m.getSubjectId());
            });
            repository.deleteBySubjectList(result.getId(), subjectIdList);
            employeePaperSubjects = this.saveAll(employeePaperSubjectList);
        }
        if (employeePaperStageDetailVO != null ) {
            // 提交试卷 修改学习阶段对应的状态 && result.getAdoptScore() != null && result.getGrade() >= 0 && result.getGrade() >= result.getAdoptScore()
            stageDetailRepository.updateUserFinishState(employeePaperStageDetailVO.getStageDetailId().longValue(), employeePaper.getEmployeeId());

            planService.updatePlanFinishState(employeePaperStageDetailVO.getPlanId());
        }

        return employeePaperSubjects;
    }


    public List<Map<String, Object>> findByEmployeePaperId(Long employeePaperId) {
        return repository.selectEmployeePaperSubjectByEmployeePaperId(employeePaperId);
    }
}
